package com.hua.common.commonview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.hua.common.commontools.DipUtil;
/**
 * 用于显示百分比的圆环控件
 * @author huaxiaolin_pc
 *
 */
public class RingControl extends LinearLayout {

	/**
	 * 背景圆环画笔
	 */
	private Paint backPaint = null;
	/**
	 * 控件自身的宽
	 */
	private int mWidth = 0;

	/**
	 * 当前的已经绘制的角度数
	 */
	private float nowCount = 0;

	private int areaColors[] = null;
	/**
	 * 用于将百分比转换为角度
	 */
	private float areas[] = null;

	/**
	 * 现在绘制的第几个区域
	 */
	private int areaIndex = 1;

	private ValueAnimator anim = null;

	private int stat = 0;
	/**
	 * 圆环画笔的宽度
	 */
	private float paintWidth=0f;
	public RingControl(Context context) {
		super(context);
		this.init();
	}

	public RingControl(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.init();
	}

	public RingControl(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.init();
	}

	private void init() {
		this.initPaint();
		this.initAnim(); // 初始化动画
	}

	private void initPaint() {
		this.paintWidth= DipUtil.Dp2px(getContext(), 10);
		backPaint = new Paint();
		backPaint.setAntiAlias(true);
		backPaint.setStyle(Style.STROKE);
		backPaint.setStrokeWidth(this.paintWidth);
		backPaint.setAntiAlias(true); // 消除锯齿
		backPaint.setDither(true);
		backPaint.setStrokeCap(Cap.SQUARE); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
		// Cap.ROUND,或方形样式Cap.SQUARE
		backPaint.setColor(Color.parseColor("#ffc0c0c0"));
	}

	/**
	 * 初始化动画对象
	 */
	private void initAnim() {
		anim = ValueAnimator.ofFloat(0, 360);
		anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				nowCount = (float) animation.getAnimatedValue();
				refreshDraw();
			}
		});
		anim.setDuration(36 * 100);
	}

	/**
	 * 设置每个区域的百分比 然后根据百分比算出每个区域的度数
	 *
	 * @param areaCount
	 */
	public void setareaCounts(float areaCount[]) {
		this.areas = new float[areaCount.length];
		for (int i = 0; i < areaCount.length; i++) {
			float count = areaCount[i] * 360;
			if (count <= 1) {
				this.areas[i] = 1;
			} else {
				this.areas[i] = count;
			}
		}
	}

	public void setColors(int colors[]) {
		this.areaColors = colors;
	}

	/**
	 * 开始绘制
	 */
	public void start() {
		this.reset();
		nowCount = 0;
		areaIndex = 1;
		this.stat = 1;
		if (anim == null) {
			this.initAnim();
		}
		anim.start();
	}

	/**
	 * 重置
	 */
	public void reset() {
		this.cancel();
		this.stat = 0;
		this.invalidate();
	}

	/**
	 * 暂停
	 */
	public void cancel() {
		this.stat = 0;
		if (anim == null) {
			this.initAnim();
		}
		anim.cancel();
		this.invalidate();
	}

	/**
	 * 刷新绘制图标
	 */
	private void refreshDraw() {
		this.invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		drawAcr(canvas);
		if (stat == 1) {
			drawEveryArea(canvas);
		}
	}

	/**
	 * 绘制背景圆环
	 *
	 * @param canvas
	 */
	private void drawAcr(Canvas canvas) {
		RectF oval1 = new RectF(20, 20, mWidth - 20, mWidth - 20);
		canvas.drawArc(oval1, 0, 360, false, backPaint);// 小弧形
	}

	/**
	 * 绘制每一个区域
	 */
	private void drawEveryArea(Canvas canvas) {
		float tempCount = 0;
		for (int i = 0; i < areaIndex; i++) {
			tempCount = tempCount + areas[i];
		}
		if (nowCount > tempCount) {
			if (areaIndex < areas.length) {
				areaIndex++;
			}
		}
		drawArea(canvas, areaIndex);

	}

	private void drawArea(Canvas canvas, int index) {
		for (int i = 0; i < index; i++) {
			if (i == 0) {
				RectF oval1 = new RectF(20, 20, mWidth - 20, mWidth - 20);
				canvas.drawArc(oval1, 0, nowCount, false,
						initEveryAreaPaint(this.areaColors[0]));// 小弧形
			} else {
				RectF oval1 = new RectF(20, 20, mWidth - 20, mWidth - 20);
				canvas.drawArc(oval1, getCount(i), nowCount - getCount(i),
						false, initEveryAreaPaint(this.areaColors[i]));// 小弧形
			}

		}
	}

	private float getCount(int index) {
		float count = 0f;
		if (index == 0) {
			return count;
		} else {
			for (int i = 0; i <= index - 1; i++) {
				count = count + areas[i];
			}
			return count;
		}

	}

	/**
	 * 初始化每个区域画笔
	 */
	private Paint initEveryAreaPaint(int color) {
		Paint AreaPaint = new Paint();
		AreaPaint.setAntiAlias(true);
		AreaPaint.setStyle(Style.STROKE);
		AreaPaint.setStrokeWidth(this.paintWidth);
		AreaPaint.setAntiAlias(true); // 消除锯齿
		AreaPaint.setDither(true);
		AreaPaint.setStrokeCap(Cap.BUTT); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
		// Cap.ROUND,或方形样式Cap.SQUARE
		AreaPaint.setColor(color);
		return AreaPaint;
	}

	/**
	 * 获取控件自身的宽度
	 */
	private void initWH() {
		mWidth = getMeasuredWidth();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		this.initWH();
	}
}
